package com.springboot.service.impl;

import com.springboot.mapper.DoctorInfoFormMapper;
import com.springboot.mapper.OrderFormMapper;
import com.springboot.mapper.PatientInfoFormMapper;
import com.springboot.mapper.UserLoginFormMapper;
import com.springboot.model.DoctorInfoForm;
import com.springboot.model.OrderForm;
import com.springboot.model.PatientInfoForm;
import com.springboot.model.UserLoginForm;
import com.springboot.service.DoctorViewAppointDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 医生查看预约详情
 */
@Service
public class DoctorViewAppointDetailsImpl implements DoctorViewAppointDetailsService
{
    @Autowired
    private OrderFormMapper orderFormMapper;

    @Autowired
    private PatientInfoFormMapper patientInfoFormMapper;

    @Autowired
    private UserLoginFormMapper userLoginFormMapper;

    @Autowired
    private DoctorInfoFormMapper doctorInfoFormMapper;

    //通过医生id查找预约了该医生的预约记录列表
    @Override
    public ArrayList<OrderForm> findOrderBydoctorId(Integer id)
    {
        ArrayList<OrderForm> orderForms=orderFormMapper.selectByDoctorId(id);
        return orderForms;
    }

    //通过预约id查找患者
    @Override
    public PatientInfoForm findPatientByBookId(Integer id)
    {
        PatientInfoForm patientInfoForm=patientInfoFormMapper.selectByPrimaryKey(id);
        return patientInfoForm;
    }

    //通过用户id查找该用户
    @Override
    public UserLoginForm findUserLogo(Integer id)
    {
        UserLoginForm userLoginForm=userLoginFormMapper.selectByPrimaryKey(id);
        return userLoginForm;
    }

    //通过手机号码查找医生
    @Override
    public DoctorInfoForm findDoctorByTel(String tel)
    {
        DoctorInfoForm doctorInfoForm=doctorInfoFormMapper.selectByTel(tel);
        return doctorInfoForm;
    }


    //根据医生Id查找已逾期的预约表单
    @Override
    public ArrayList<OrderForm> findOverOrderByDoctor(Integer doctorId) {
        ArrayList<OrderForm> orderForms = new ArrayList<>();
        try {
            ArrayList<OrderForm> orderForms1 = orderFormMapper.selectByDoctorId(doctorId);
            for(int i=0;i<orderForms1.size();i++)
            {
                OrderForm orderForm = orderForms1.get(i);
                Date utilDate = new Date();
                java.sql.Date today = new java.sql.Date(utilDate.getTime());
                if(orderForm.getBootDate().before(today)&&orderForm.getProgress().equals("未就诊"))
                {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//日期格式
                    String current_date=format.format(System.currentTimeMillis());
                    String book_date=format.format(orderForm.getBootDate());
                    if(!book_date.equals(current_date))
                    {
                        orderForms.add(orderForm);
                    }
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("查找不到该医生Id的预约表单");
        }
        return orderForms;
    }
}
